﻿using Core.Log;
using Core.Utils;
using UnityEngine;

namespace Core.Animation {
    public class AnimationState {
        private AnimationProcessTaskQueue mTaskQueue;

        public AnimationState(GameObject obj) {
            mTaskQueue = new AnimationProcessTaskQueue();
            AnimationSystem.Ins.Add(obj,this);
        }
        
        public AnimationProcess SetAnimation(IAnimation anim, string name, bool isLoop) {
            //先判断是否存在该动画
            if (!anim.HasClip(name)) {
                ADbg.LogEF("[Anim] 动画 {0} 不存在，但在试图播放",name);
                return null;
            }
            mTaskQueue.Clear();
            AnimationProcess process = PoolMgr.Ins.Get<AnimationProcess>();
            process.Reset();
            process.SetData(anim,name,isLoop);
            mTaskQueue.AddLast(process);
            anim.Play(name,isLoop);
            Updater(0);
            return process;
        }

        public AnimationProcess AddAnimation(IAnimation anim, string name, bool isLoop,float delay) {
            if (!anim.HasClip(name)) {
                ADbg.LogEF("[Anim] 动画 {0} 不存在，但在试图播放",name);
                return null;
            }
            AnimationProcess process = PoolMgr.Ins.Get<AnimationProcess>();
            process.Reset();
            process.SetData(anim,name,isLoop,delay);
            mTaskQueue.AddLast(process);
            anim.Play(name,isLoop);
            return null;
        }
        public void Updater(float deltaTime) {
            for (int i = 0; i < mTaskQueue.TaskQueue.Count; ++i) {
                if (mTaskQueue.TaskQueue[i].IsEnd) {
                    continue;
                }
                mTaskQueue.TaskQueue[i].Update(deltaTime);
            }
        }
    }
}